/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zb.hms.business.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.hms.base.util.messageUtil.ResultMessage;
import com.zb.hms.base.util.stringUtil.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 *  对所有服务器请求拦截
 * @Description
 *  @author zb
 * @datetime 2017-7-30 21:00:04
 * @since JDK 1.8
 * @version V1.0
 */
public class AllInterceptor implements HandlerInterceptor {

    @Resource
    private ResultMessage resultMessage;
    @Resource
    private ObjectMapper objectMapper;

    private HttpSession session;
    private String url;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        session = request.getSession();
        url = request.getRequestURI();
        //如果访问首页的时候则放行
        if ("/HMS/".equals(url)) {
            return true;
        }
        {
            if (!StringUtil.isUrl(url)) {
                return true;
            }
        }
//        {            
//            if (StringUtil.isPub(url) ) {
//                return true;
//            } else {
//                if (null == session.getAttribute(SessionConstants.USER)) {
//                    return false;
//                } else {
//                    resultMessage.setStatus(100).setValidataResult("请登录!");
//                    try {
//                        response.getWriter().print(objectMapper.writeValueAsString(resultMessage));
//                    } catch (JsonProcessingException ex) {
//                        Logger.getLogger(AllInterceptor.class.getName()).log(Level.SEVERE, null, ex);
//                    } catch (IOException ex) {
//                        Logger.getLogger(AllInterceptor.class.getName()).log(Level.SEVERE, null, ex);
//                    }
////                    baseController.setLastUrl(request);
//                    return false;
//                }
//            }
//        }

//        
//        System.out.println(url);  
//        if("/HMS/".equals(url))
//            return false;
//        //判断是否需要登录
//        {
//            if(StringUtil.isPub(url))S
//                return true;
//            else
//               if( null==session.getAttribute(SessionConstants.USER))
//               {
//                     resultMessage.setStatus(200).setValidataResult("请登录!");
//                     response.getWriter().print(objectMapper.writeValueAsString(resultMessage));
//                     response.sendRedirect("/veiw/main.html");
//                     return false;
//               }
//        }
        //验证码拦截
//        {
//         if(url.startsWith("11")){
//                if(null==session.getAttribute(SessionManage.VERIFYCODE)){
//                    resultMessage.setStatus(200).setValidataResult("验证码失效，请刷新验证码");
//                    response.getWriter().print(objectMapper.writeValueAsString(resultMessage));
//                    return false;
//                }
//                else{
//                    String verify=request.getParameter("verify");
//                    if(session.getAttribute(SessionManage.VERIFYCODE).equals(verify))
//                        return true;
//                    else{
//                          resultMessage.setStatus(200).setValidataResult("验证码输入错误");
//                          response.getWriter().print(objectMapper.writeValueAsString(resultMessage));
//                          return false;
//                    }               
//                }
//                    
//            }        
//         } 
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

}
